﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FlickrNet;
using System.Timers;

namespace Cheesr
{
    public partial class Browser : Form
    {
        #region Fields

        private const string _apiKey = "9481b39072b61246410278f0177f94ea";
        private Flickr _flickr;
        private Photos _photos;
        private PhotoCollection _photoCollection;
        private PhotoSearchOptions _photoSearchOptions;
        private int count;
        private string _query;
        private int currentPhotoIndex;

        #endregion

        #region Constructor

        public Browser()
        {
            InitializeComponent();
            Flickr = new Flickr(_apiKey);
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the flickr.
        /// </summary>
        /// <value>The flickr.</value>
        public Flickr Flickr
        {
            get
            {
                return _flickr;
            }
            protected set
            {
                _flickr = value;
            }
        }

        /// <summary>
        /// Gets or sets the query.
        /// </summary>
        /// <value>The query.</value>
        public string Query
        {
            get
            {
                return _query;
            }
            protected set
            {
                _query = value;
            }
        }

        /// <summary>
        /// Gets or sets the photos.
        /// </summary>
        /// <value>The photos.</value>
        public Photos Photos
        {
            get
            {
                return _photos;
            }
            protected set
            {
                _photos = value;
            }
        }

        /// <summary>
        /// Gets or sets the photo collection.
        /// </summary>
        /// <value>The photo collection.</value>
        public PhotoCollection PhotoCollection
        {
            get
            {
                return _photoCollection;
            }
            protected set
            {
                _photoCollection = value;
            }
        }

        /// <summary>
        /// Gets or sets the photo search options.
        /// </summary>
        /// <value>The photo search options.</value>
        public PhotoSearchOptions PhotoSearchOptions
        {
            get
            {
                return _photoSearchOptions;
            }
            protected set
            {
                _photoSearchOptions = value;
            }
        }

        #endregion

        #region Helper Methods

        #endregion

        private void ClearPhotos()
        {
            pictureBox1.Image = null;
            pictureBox2.Image = null;
            pictureBox3.Image = null;
            pictureBox4.Image = null;
            pictureBox5.Image = null;
            pictureBox6.Image = null;
            pictureBox7.Image = null;
            pictureBox8.Image = null;
            pictureBox9.Image = null;
            pictureBox10.Image = null;
            largePhotoBox.Image = null;
        }

        private void LoadPhotos()
        {
            count = PhotoCollection.Count;
            if (count == 0) statusLabel.Text = "No results.";
            if (count > 0) pictureBox1.Load(PhotoCollection[0].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 1) pictureBox2.Load(PhotoCollection[1].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 2) pictureBox3.Load(PhotoCollection[2].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 3) pictureBox4.Load(PhotoCollection[3].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 4) pictureBox5.Load(PhotoCollection[4].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 5) pictureBox6.Load(PhotoCollection[5].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 6) pictureBox7.Load(PhotoCollection[6].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 7) pictureBox8.Load(PhotoCollection[7].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 8) pictureBox9.Load(PhotoCollection[8].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 9) pictureBox10.Load(PhotoCollection[9].ThumbnailUrl);
            progressBar.Value += 9;
            if (count > 0) largePhotoBox.Load(PhotoCollection[0].MediumUrl);
            progressBar.Value = 100;
        }

        #region Event Handlers

        private void searchButton_Click(object sender, EventArgs e)
        {
            ClearPhotos();
            Query = searchBox.Text.Trim();
            statusLabel.Text = Query;
            progressBar.Value = 0;
            PhotoSearchOptions = new PhotoSearchOptions();
            PhotoSearchOptions.SortOrder = PhotoSearchSortOrder.Relevance;
            PhotoSearchOptions.Tags = Query;
            PhotoSearchOptions.PerPage = 10;
            if (anyTagsRadioButton.Checked)
            {
                PhotoSearchOptions.TagMode = TagMode.AnyTag;
            }
            else 
            {
                PhotoSearchOptions.TagMode = TagMode.AllTags;
            }
            try
            {
                Photos = Flickr.PhotosSearch(PhotoSearchOptions);
                PhotoCollection = Photos.PhotoCollection;
                LoadPhotos();
            }catch (Exception E)
            {
                MessageBox.Show("Paramaterless searches have been disabled", "Error", MessageBoxButtons.OK);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[0].MediumUrl);
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[1].MediumUrl);
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[2].MediumUrl);
        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[3].MediumUrl);
        }

        private void pictureBox5_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[4].MediumUrl);
        }

        private void pictureBox6_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[5].MediumUrl);
        }

        private void pictureBox7_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[6].MediumUrl);
        }

        private void pictureBox8_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[7].MediumUrl);
        }

        private void pictureBox9_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[8].MediumUrl);
        }

        private void pictureBox10_Click(object sender, EventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[9].MediumUrl);
        }

        private void saveImageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            saveFileDialog1.ShowDialog();
            largePhotoBox.Image.Save(saveFileDialog1.FileName);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Dispose();
        }

        private void nextButton_Click(object sender, EventArgs e)
        {
            if (PhotoSearchOptions != null)
            {
                ClearPhotos();
                progressBar.Value = 0;
                PhotoSearchOptions.Page++;
                Photos = Flickr.PhotosSearch(PhotoSearchOptions);
                PhotoCollection = Photos.PhotoCollection;
                LoadPhotos();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            MessageBox.Show("DEMMS Assessed Excercise Solution, November 2008", "About", MessageBoxButtons.OK);
        }

        #endregion

        private void SlideShowButton_Click(object sender, EventArgs e)
        {
            if (PhotoSearchOptions != null)
            {
                currentPhotoIndex = 0;
                System.Timers.Timer slideShowtimer = new System.Timers.Timer();
                slideShowtimer.Elapsed += new ElapsedEventHandler(OnElapsed);
                slideShowtimer.Interval = 5000;
                slideShowtimer.AutoReset = true;
                slideShowtimer.Start();
            }
        }

        void OnElapsed(Object sender, ElapsedEventArgs e)
        {
            largePhotoBox.Load(PhotoCollection[currentPhotoIndex].MediumUrl);
            currentPhotoIndex++;
        } 

    }
}
